/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.Symptom;
import com.mvc.service.healthmanager.SymptomServiceI;

/**
 * @author qingjie
 *
 */
@RequestMapping("/sympton")
@Controller
public class SymptonController {

	@Autowired
	private SymptomServiceI ssi;
	
	@RequestMapping("/saveSympton")
	@ResponseBody
	public Json saveTeeth(HttpServletRequest request,Symptom symptom) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		Symptom sy = new Symptom();
		if (!json.isEmpty()) {
			sy = JSON.parseObject(json, Symptom.class);
			sy.setId(null);
		}
		symptom = ssi.saveOrUpdate(sy);
		Json j = new Json();
		if(symptom!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(symptom);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
//	@RequestMapping(value="/saveSympton", method=RequestMethod.POST,consumes = "application/json")
//	@ResponseBody
//	public Json saveTeeth(@RequestBody Symptom sy,HttpServletRequest request,Symptom symptom) throws Exception{
//		sy.setId(null);
//		symptom = ssi.saveOrUpdate(sy);
//		Json j = new Json();
//		if(symptom!=null){
//			j.setMsg("保存成功");
//			j.setSuccess(true);
//			j.setObj(symptom);
//		}else{
//			j.setMsg("保存失败");
//			j.setSuccess(false);
//		}
//		return j;
//	}
	
	@RequestMapping(value="/saveSympton", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveTeeth(@RequestBody List<Symptom> sys,HttpServletRequest request) throws Exception{
		System.out.println("未转化数据："+JSON.toJSONString(sys));
		List<Symptom> list = new ArrayList<Symptom>();
		Json j = new Json();
		if(sys.size()>0){
			for(Symptom sy : sys){
				Long appLocalId = ValidatorUtil.isNullOnLong(sy.getId())?null:sy.getId();
				sy.setId(null);
				Symptom symptom = new Symptom();
				symptom = ssi.saveOrUpdate(sy);
				if(symptom!=null&&!ValidatorUtil.isNullOnLong(symptom.getId())){
					symptom.setRespCode(0);
				}else{
					symptom.setRespCode(-1);
				}
				symptom.setAppLocalId(appLocalId);
				list.add(symptom);
			}
			j.setMsg("数据已处理");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
}
